package org.kjtc.controller;

import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kjtc.entity.CarInfo;
import org.kjtc.entity.ChargingStation;
import org.kjtc.entity.ChargingStationStatistics;
import org.kjtc.entity.ChargingStationStatus;
import org.kjtc.entity.DeviceObject;
import org.kjtc.entity.Equipment;
import org.kjtc.entity.EquipmentStatus;
import org.kjtc.entity.Page;
import org.kjtc.entity.User;
import org.kjtc.entity.UserInfo;
import org.kjtc.mongoEntity.Log;
import org.kjtc.mongoService.LogService;
import org.kjtc.service.CarInfoService;
import org.kjtc.service.ChargingStationService;
import org.kjtc.service.ChargingStationStatisticsService;
import org.kjtc.service.EquipmentService;
import org.kjtc.service.StatuteManagement;
import org.kjtc.service.UserInfoService;
import org.kjtc.socket.MessageRegistration;
import org.kjtc.util.Const;
import org.kjtc.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RealTimeStatusController extends BaseController {

    @Resource(name = "deviceObject")
    private DeviceObject deviceObject;

    @Resource(name = "userInfoService")
	private UserInfoService userInfoService;

    @Resource(name = "equipmentService")
    private EquipmentService equipmentService;

    @Resource(name = "chargingStationService")
	private ChargingStationService chargingStationService;

	@Resource(name = "chargingStationStatisticsService")
	private ChargingStationStatisticsService chargingStationStatisticsService;

	@Resource(name = "logService")
	private LogService logService;

	@Resource(name = "carInfoService")
	private CarInfoService carInfoService;

    @RequestMapping(value = "/getMenuData")
    @ResponseBody
    public Map<String, Object> getMenuData(){

    	Map<String, Object> map = new HashMap<String, Object>();
//    	String pattern = "yyyy年 MM月 dd日 EE hh:mm:ss aa";
//    	String pattern = "yyyy年 MM月 dd日 EE HH:mm:ss";
//		SimpleDateFormat df = new SimpleDateFormat(pattern);
		Date date = new Date();
//		String time = df.format(date);
//		map.put("time", time);
		map.put("date", date);
		if (this.getUesr() != null) {
			User user = this.getUesr();
			if (user.getUserID() == null || user.getUserID().isEmpty()) {
				map.put("userName", "未知");
			} else {
				UserInfo userInfo = new UserInfo();
				userInfo.setUserID(user.getUserID());
				try {
					List<UserInfo> userInfoList = userInfoService.getUserInfo(userInfo);
					if (userInfoList.size() == 1) {
						this.getSession().setAttribute(Const.SESSION_USERINFO, userInfoList.get(0));
						map.put("userName", this.getUesrInfo().getUserName());
						map.put("level", this.getUesrInfo().getLevel());
						logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取系统数据：" + map.toString());
					} else {
						map.put("userName", "未知");
					}
				} catch (Exception e) {
					e.printStackTrace();
					map.put("userName", "未知");
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取系统数据异常：" + e.getMessage());
				}
			}
		} else {
			map.put("userName", "未知");
		}

		date = null;
    	return map;
    }

	@RequestMapping(value = "realTimeEquipment")
	@ResponseBody
    public Object getEquipmentStatus(Equipment equipment, String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		if (page == null || page.isEmpty()) {
			page = "1";
		}
		if (rows == null || rows.isEmpty()) {
			rows = "1";
		}
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<Equipment> equipmentList = null;
		EquipmentStatus equipmentStatus = null;
		String chargingStationID = null;
		String equipmentID = null;
		ChargingStationStatus chargingStationStatus = null;
		Map<String, Object> subStationMap = deviceObject.getSubStationMap();
		pageQuery.setTotalPage(0);
		pageQuery.setTotalResult(0);

		String equipmentNoList = "";
		try {
			int totalOnlineCount = 0;
			int totalChargingCount = 0;
			int totalChargedCount = 0;
			int totalIdleCount = 0;
			int totalFaultCount = 0;
			if (subStationMap != null && subStationMap.size() > 0) {
				for (Object chargingStationRequest : subStationMap.values()) {
					chargingStationStatus = (ChargingStationStatus)chargingStationRequest;
					JSONArray equipmentData = chargingStationStatus.getEquipmentData();
					if (equipmentData != null) {
						int length = equipmentData.size();
						JSONObject equipmentJson = null;
						for (int i = 0; i < length; i++) {
							equipmentJson = equipmentData.getJSONObject(i);
							if (equipmentJson != null) {
								if (equipmentJson.has("equipmentID")) {
									if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals(Const.EQUIPMENT_ONLINE)) {
										if (equipmentNoList.equals("")) {
											equipmentNoList = equipmentNoList + equipmentJson.getString("equipmentID");
										} else {
											equipmentNoList = equipmentNoList + "," + equipmentJson.getString("equipmentID");
										}
										if ((equipment.getChargingStationID() == null || equipment.getChargingStationID().isEmpty() || equipment.getChargingStationID().equals(chargingStationStatus.getChargingStationID())) && !"0".equals(equipment.getCommunicationState())) {
											totalOnlineCount ++;
											if (equipmentJson.has("deviceState")) {
												if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING)) {
													totalChargingCount++;
												} else if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGED)) {
													totalChargedCount++;
												} else if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_IDLE)) {
													totalIdleCount++;
												} else if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_FAULT)) {
													totalFaultCount++;
												}
											}
										}
									}
								}
							}
						}
						equipment.setEquipmentIDList(equipmentNoList.split(","));
					}
				}
			}
			equipmentList = equipmentService.getEquipmentPage(equipment, pageQuery);
			List<EquipmentStatus> equipmentStatusList = new ArrayList<EquipmentStatus>();
			for (Equipment equipmentPage : equipmentList) {
				if ("1".equals(equipmentPage.getActive())) {
					equipmentStatus = new EquipmentStatus();
					equipmentStatus.setChargingStationID(equipmentPage.getChargingStationID());
					equipmentStatus.setChargingStationName(equipmentPage.getChargingStationName());
					equipmentStatus.setEquipmentID(equipmentPage.getEquipmentID());
					equipmentStatus.setEquipmentNo(equipmentPage.getEquipmentNo());
					equipmentStatus.setEquipmentName(equipmentPage.getEquipmentName());
//					equipmentStatus.setEquipmentType(equipmentPage.getEquipmentType());
//					equipmentStatus.setEquipmentTypeName(equipmentPage.getEquipmentTypeName());
					equipmentStatus.setEquipmentPowerType(equipmentPage.getEquipmentPowerType());
					equipmentStatus.setEquipmentPowerTypeName(equipmentPage.getEquipmentPowerTypeName());
					equipmentStatus.setChargingGun("-");
					equipmentStatus.setCommunicationState("断开");
//					equipmentStatus.setDeviceState("-");
//					equipmentStatus.setCarNumber("-");
//					equipmentStatus.setCarNo("-");
//					equipmentStatus.setCardNo("-");
//					equipmentStatus.setOutputVoltage("0");
//					equipmentStatus.setOutputElectric("0");
//					equipmentStatus.setOutputPower("0");
//					equipmentStatus.setCurrentSOC("0");
//					equipmentStatus.setChargedKWH("-");
//					equipmentStatus.setChargedTime("-");
//					equipmentStatus.setStartSOC("0");
					chargingStationID = equipmentPage.getChargingStationID();
					if (subStationMap != null && subStationMap.containsKey(chargingStationID)){
						chargingStationStatus = (ChargingStationStatus)subStationMap.get(chargingStationID);
						map.put("requestDTTM", Tools.date2Str(chargingStationStatus.getRequestDTTM()));
						equipmentID = equipmentPage.getEquipmentNo();
						JSONArray equipmentData = chargingStationStatus.getEquipmentData();
						if (equipmentData != null) {
							int length = equipmentData.size();
							JSONObject equipmentJson = null;
							for (int i = 0; i < length; i++) {
								equipmentJson = equipmentData.getJSONObject(i);
								if (equipmentJson != null) {
									if (equipmentJson.has("equipmentID") && equipmentJson.getString("equipmentID").equals(equipmentID)) {
										if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals(Const.EQUIPMENT_ONLINE)) {
											equipmentStatus.setCommunicationState(equipmentJson.getString("communicationState"));
											if (equipmentJson.has("deviceState")) {
												equipmentStatus.setDeviceState(equipmentJson.getString("deviceState"));
												if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING)) {
													if (equipmentJson.has("chargingGun")) {
														equipmentStatus.setChargingGun(equipmentJson.getString("chargingGun"));
													}
//													if (equipmentJson.has("carNumber")) {
//														equipmentStatus.setCarNumber(equipmentJson.getString("carNumber"));
//													}
//													if (equipmentJson.has("carNo")) {
//														equipmentStatus.setCarNo(equipmentJson.getString("carNo"));
//													}
//													if (equipmentJson.has("cardNo")) {
//														equipmentStatus.setCardNo(equipmentJson.getString("cardNo"));
//													}
//													if (equipmentJson.has("outputVoltage")) {
//														equipmentStatus.setOutputVoltage(equipmentJson.getString("outputVoltage"));
//													}
//													if (equipmentJson.has("outputElectric")) {
//														equipmentStatus.setOutputElectric(equipmentJson.getString("outputElectric"));
//													}
//													if (equipmentStatus.getOutputElectric() != null && !equipmentStatus.getOutputElectric().isEmpty() &&
//															equipmentStatus.getOutputVoltage() != null && !equipmentStatus.getOutputVoltage().isEmpty()) {
//														BigDecimal outputPower = new BigDecimal(Double.valueOf(equipmentStatus.getOutputElectric()) * Double.valueOf(equipmentStatus.getOutputVoltage()) / 1000);
//														equipmentStatus.setOutputPower(String.valueOf(outputPower.setScale(2, BigDecimal.ROUND_HALF_UP)));
//													}
//													if (equipmentJson.has("currentSOC")) {
//														equipmentStatus.setCurrentSOC(equipmentJson.getString("currentSOC"));
//													}
//													if (equipmentJson.has("chargedKWH")) {
//														BigDecimal outputPower = new BigDecimal(Double.valueOf(equipmentJson.getString("chargedKWH")));
//														equipmentStatus.setChargedKWH(String.valueOf(outputPower.setScale(1, BigDecimal.ROUND_HALF_UP)));
//													}
//													if (equipmentJson.has("startSOC")) {
//														equipmentStatus.setStartSOC(equipmentJson.getString("startSOC"));
//													}
//													if (equipmentJson.has("startChargingDTTM")) {
//														equipmentStatus.setStartChargingDTTM(equipmentJson.getString("startChargingDTTM"));
//														if (Tools.str2DateTime(equipmentJson.getString("startChargingDTTM")) != null) {
//															equipmentStatus.setChargedTime(Tools.formatTime(((new Date()).getTime() - Tools.str2DateTime(equipmentJson.getString("startChargingDTTM")).getTime()) / 1000));
//														}
//													}
//													if (equipmentJson.has("startAmmeterNo")) {
//														equipmentStatus.setStartAmmeterNo(equipmentJson.getString("startAmmeterNo"));
//													}
												} else if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGED)) {
													if (equipmentJson.has("chargingGun")) {
														equipmentStatus.setChargingGun(equipmentJson.getString("chargingGun"));
													}
												}
											}
										}
										break;
									}
								}
							}
						}
					}
					equipmentStatusList.add(equipmentStatus);
				}
			}
			map.put("result", "Sucess");
			map.put("totalCnt", pageQuery.getTotalResult());
			map.put("totalPage", pageQuery.getTotalPage());
			map.put("equipmentStatusList", equipmentStatusList);
			map.put("totalOfflineCount", pageQuery.getTotalResult() - totalOnlineCount);
			map.put("totalOnlineCount", totalOnlineCount);
			map.put("totalChargingCount", totalChargingCount);
			map.put("totalChargedCount", totalChargedCount);
			map.put("totalIdleCount", totalIdleCount);
			map.put("totalFaultCount", totalFaultCount);
			equipmentStatusList = null;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩实时状态信息获取异常:" + e.getMessage());
		}
		equipmentList = null;
		equipmentStatus = null;
		chargingStationID = null;
		equipmentID = null;
		chargingStationStatus = null;
		pageQuery = null;
		return map;
    }

	@RequestMapping(value = "realTimeEquipmentDetail")
	@ResponseBody
    public Object getEquipmentDetailStatus(Equipment equipment) {

		Map<String, Object> map = new HashMap<String, Object>();

		Equipment equipmentDetail = null;
		EquipmentStatus equipmentStatus = null;
		ChargingStationStatus chargingStationStatus = null;
		Map<String, Object> subStationMap = deviceObject.getSubStationMap();

		try {
			equipmentDetail = equipmentService.getEquipmentDetail(equipment);
			if ("1".equals(equipmentDetail.getActive()) && ((equipmentDetail.getEquipmentID() != null && !equipmentDetail.getEquipmentID().isEmpty()) || (equipmentDetail.getEquipmentNo() != null && !equipmentDetail.getEquipmentNo().isEmpty()))) {
				equipmentStatus = new EquipmentStatus();
				equipmentStatus.setChargingStationID(equipmentDetail.getChargingStationID());
				equipmentStatus.setChargingStationName(equipmentDetail.getChargingStationName());
				equipmentStatus.setEquipmentID(equipmentDetail.getEquipmentID());
				equipmentStatus.setEquipmentNo(equipmentDetail.getEquipmentNo());
				equipmentStatus.setEquipmentName(equipmentDetail.getEquipmentName());
				equipmentStatus.setEquipmentType(equipmentDetail.getEquipmentType());
				equipmentStatus.setEquipmentTypeName(equipmentDetail.getEquipmentTypeName());
				equipmentStatus.setEquipmentPowerType(equipmentDetail.getEquipmentPowerType());
				equipmentStatus.setEquipmentPowerTypeName(equipmentDetail.getEquipmentPowerTypeName());
				equipmentStatus.setChargingGun("-");
				equipmentStatus.setCommunicationState("断开");
				equipmentStatus.setDeviceState("-");
				equipmentStatus.setCarNumber("-");
				equipmentStatus.setCarNo("-");
				equipmentStatus.setCardNo("-");
				equipmentStatus.setOutputVoltage("0");
				equipmentStatus.setOutputElectric("0");
				equipmentStatus.setOutputPower("0");
				equipmentStatus.setCurrentSOC("0");
				equipmentStatus.setChargedKWH("-");
				equipmentStatus.setChargedTime("-");
				equipmentStatus.setStartSOC("0");
				if (subStationMap != null && subStationMap.containsKey(equipmentDetail.getChargingStationID())){
					chargingStationStatus = (ChargingStationStatus)subStationMap.get(equipmentDetail.getChargingStationID());
					map.put("requestDTTM", Tools.date2Str(chargingStationStatus.getRequestDTTM()));
					JSONArray equipmentData = chargingStationStatus.getEquipmentData();
					if (equipmentData != null) {
						int length = equipmentData.size();
						JSONObject equipmentJson = null;
						for (int i = 0; i < length; i++) {
							equipmentJson = equipmentData.getJSONObject(i);
							if (equipmentJson != null) {
								if (equipmentJson.has("equipmentID") && equipmentJson.getString("equipmentID").equals(equipmentDetail.getEquipmentNo())) {
									if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals(Const.EQUIPMENT_ONLINE)) {
										equipmentStatus.setCommunicationState(equipmentJson.getString("communicationState"));
										if (equipmentJson.has("deviceState")) {
											equipmentStatus.setDeviceState(equipmentJson.getString("deviceState"));
											if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING)) {
												if (equipmentJson.has("chargingGun")) {
													equipmentStatus.setChargingGun(equipmentJson.getString("chargingGun"));
												}
												if (equipmentJson.has("carNumber")) {
													equipmentStatus.setCarNumber(equipmentJson.getString("carNumber"));
													if (equipmentJson.getString("carNumber") != null && !equipmentJson.getString("carNumber").isEmpty()) {
														CarInfo carInfo = new CarInfo();
														carInfo.setCarNumber(String.valueOf(Integer.parseInt(equipmentJson.getString("carNumber"))));
														List<CarInfo> carInfoList = carInfoService.getCarInfo(carInfo);
														if (carInfoList != null && carInfoList.size() > 0) {
															equipmentStatus.setCarNo(carInfoList.get(0).getCarNo());
														}
														carInfoList = null;
													}
												}
//												if (equipmentJson.has("carNo")) {
//													equipmentStatus.setCarNo(equipmentJson.getString("carNo"));
//												}
												if (equipmentJson.has("cardNo")) {
													equipmentStatus.setCardNo(equipmentJson.getString("cardNo"));
												}
												if (equipmentJson.has("outputVoltage")) {
													equipmentStatus.setOutputVoltage(equipmentJson.getString("outputVoltage"));
												}
												if (equipmentJson.has("outputElectric")) {
													equipmentStatus.setOutputElectric(equipmentJson.getString("outputElectric"));
												}
												if (equipmentStatus.getOutputElectric() != null && !equipmentStatus.getOutputElectric().isEmpty() &&
														equipmentStatus.getOutputVoltage() != null && !equipmentStatus.getOutputVoltage().isEmpty()) {
													BigDecimal outputPower = new BigDecimal(Double.valueOf(equipmentStatus.getOutputElectric()) * Double.valueOf(equipmentStatus.getOutputVoltage()) / 1000);
													equipmentStatus.setOutputPower(String.valueOf(outputPower.setScale(2, BigDecimal.ROUND_HALF_UP)));
												}
												if (equipmentJson.has("currentSOC")) {
													equipmentStatus.setCurrentSOC(equipmentJson.getString("currentSOC"));
												}
												if (equipmentJson.has("chargedKWH")) {
													BigDecimal outputPower = new BigDecimal(Double.valueOf(equipmentJson.getString("chargedKWH")));
													equipmentStatus.setChargedKWH(String.valueOf(outputPower.setScale(1, BigDecimal.ROUND_HALF_UP)));
												}
												if (equipmentJson.has("startSOC")) {
													equipmentStatus.setStartSOC(equipmentJson.getString("startSOC"));
												}
												if (equipmentJson.has("startChargingDTTM")) {
													equipmentStatus.setStartChargingDTTM(equipmentJson.getString("startChargingDTTM"));
													if (!"-".equals(equipmentJson.getString("startChargingDTTM")) && Tools.str2DateTime(equipmentJson.getString("startChargingDTTM")) != null) {
														equipmentStatus.setChargedTime(Tools.formatTime(((new Date()).getTime() - Tools.str2DateTime(equipmentJson.getString("startChargingDTTM")).getTime()) / 1000));
													}
												}
												if (equipmentJson.has("startAmmeterNo")) {
													equipmentStatus.setStartAmmeterNo(equipmentJson.getString("startAmmeterNo"));
												}
											} else if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGED)) {
												if (equipmentJson.has("chargingGun")) {
													equipmentStatus.setChargingGun(equipmentJson.getString("chargingGun"));
												}
											}
										}
									}
									break;
								}
							}
						}
					}
				}
			}
			map.put("result", "Sucess");
			map.put("equipmentStatus", equipmentStatus);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩实时状态详细信息获取异常:" + e.getMessage());
		}
		equipmentDetail = null;
		equipmentStatus = null;
		chargingStationStatus = null;
		return map;
    }

	@RequestMapping(value = "realTimeCharging")
	@ResponseBody
    public Object getChargingStatus(EquipmentStatus equipmentStatus, String chargingType){

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> subStationMap = deviceObject.getSubStationMap();
		ChargingStationStatus chargingStationStatus = null;
		Log mongoLog = null;
		List<Log> mongoLogList = null;

		try {
			if ("EQUIPMENT".equals(chargingType)) {
				List<Object> outputVoltage = null;
				List<Object> outputElectric = null;
				List<Object> currentSOC = null;
				if (subStationMap != null && subStationMap.containsKey(equipmentStatus.getChargingStationID())) {
					chargingStationStatus = (ChargingStationStatus)subStationMap.get(equipmentStatus.getChargingStationID());
					JSONArray equipmentData = chargingStationStatus.getEquipmentData();
					if (equipmentData != null) {
						int length = equipmentData.size();
						JSONObject equipmentJson = null;
						for (int i = 0; i < length; i++) {
							equipmentJson = equipmentData.getJSONObject(i);
							if (equipmentJson != null) {
								if (equipmentJson.has("equipmentID") && equipmentJson.getString("equipmentID").equals(equipmentStatus.getEquipmentNo())) {
									if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals(Const.EQUIPMENT_ONLINE)) {
										if (equipmentJson.has("deviceState")) {
											if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING)) {
												if (equipmentJson.has("startChargingDTTM")) {
													mongoLog = new Log();
													String message = "";
													if (equipmentJson.getString("startChargingDTTM") == null || "".equals(equipmentJson.getString("startChargingDTTM")) || "-".equals(equipmentJson.getString("startChargingDTTM"))) {
														if (equipmentJson.has("cardNo") && equipmentJson.getString("cardNo") != null && !equipmentJson.getString("cardNo").isEmpty() && !"-".equals(equipmentJson.getString("cardNo"))) {
															message = message + "\"cardNo\":\"" + equipmentJson.getString("cardNo") + ",";
														}
														if (equipmentJson.has("carNumber") && equipmentJson.getString("carNumber") != null && !equipmentJson.getString("carNumber").isEmpty() && !"-".equals(equipmentJson.getString("carNumber"))) {
															message = message + "\"carNumber\":\"" + equipmentJson.getString("carNumber") + ",";
														}
														mongoLog.setStartTime(Tools.date2Str(new Date(new Date().getTime() - 18000000)));
														mongoLog.setEndTime(Tools.date2Str(new Date()));
													} else {
														message = message + "\"startChargingDTTM\":\"" + equipmentJson.getString("startChargingDTTM") + ",";
														mongoLog.setStartTime(equipmentJson.getString("startChargingDTTM"));
													}
													if (message.isEmpty()) {
														break;
													}
													message = message + "\"equipmentID\":\"" + equipmentStatus.getEquipmentNo();
													mongoLog.setMessage(message);
													mongoLogList = logService.searchChargingLog(mongoLog);
													mongoLog = null;

													int logLength = mongoLogList.size();
													if (logLength > 0) {
														outputVoltage = new ArrayList<Object>();
														outputElectric = new ArrayList<Object>();
														currentSOC = new ArrayList<Object>();
														List<String> tempOutputVoltage = null;
														List<String> tempOutputElectric = null;
														List<String> tempCurrentSOC = null;
														for (Log log : mongoLogList) {
															JSONObject messageJson = JSONObject.fromObject(log.getMessage());
															if (messageJson.has("equipmentData")) {
																JSONArray jsonArray = messageJson.getJSONArray("equipmentData");
																int jsonLength = jsonArray.size();
																JSONObject json = null;
																for (int j = 0; j < jsonLength; j++) {
																	json = jsonArray.getJSONObject(j);
																	if (json != null && json.getString("equipmentID").equals(equipmentStatus.getEquipmentNo())) {
																		if (messageJson.has("requestDTTM")) {
																			tempOutputVoltage = new ArrayList<String>();
																			tempOutputVoltage.add(messageJson.getString("requestDTTM"));
																			if (json.has("outputVoltage")) {
																				tempOutputVoltage.add(json.getString("outputVoltage"));
																			} else {
																				tempOutputVoltage.add("0");
																			}
																			outputVoltage.add(tempOutputVoltage);
																			tempOutputVoltage = null;

																			tempOutputElectric = new ArrayList<String>();
																			tempOutputElectric.add(messageJson.getString("requestDTTM"));
																			if (json.has("outputElectric")) {
																				tempOutputElectric.add(json.getString("outputElectric"));
																			} else {
																				tempOutputElectric.add("0");
																			}
																			outputElectric.add(tempOutputElectric);
																			tempOutputElectric = null;

																			tempCurrentSOC = new ArrayList<String>();
																			tempCurrentSOC.add(messageJson.getString("requestDTTM"));
																			if (json.has("currentSOC")) {
																				tempCurrentSOC.add(json.getString("currentSOC"));
																			} else {
																				tempCurrentSOC.add("0");
																			}
																			currentSOC.add(tempCurrentSOC);
																			tempCurrentSOC = null;
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
									break;
								}
							}
						}
					}
				}
				map.put("result", "Sucess");
				map.put("outputVoltage", outputVoltage);
				map.put("outputElectric", outputElectric);
				map.put("currentSOC", currentSOC);
				outputVoltage = null;
				outputElectric = null;
				currentSOC = null;
			} else if ("STATION".equals(chargingType)) {
				List<Object> outputPower = null;
				List<Object> powerRate  = null;
				if (subStationMap != null && subStationMap.containsKey(equipmentStatus.getChargingStationID())) {
					mongoLog = new Log();
					mongoLog.setMessage("\"chargingStationID\":\"" + equipmentStatus.getChargingStationID());
					if (equipmentStatus.getStartChargingDTTM() != null && !equipmentStatus.getStartChargingDTTM().isEmpty()) {
						mongoLog.setStartTime(equipmentStatus.getStartChargingDTTM());
					} else {
						mongoLog.setStartTime(Tools.date2Str(new Date(), "yyyy-MM-dd"));
					}
					if (equipmentStatus.getEndChargingDTTM() != null && !equipmentStatus.getEndChargingDTTM().isEmpty()) {
						mongoLog.setEndTime(equipmentStatus.getEndChargingDTTM());
					}
					mongoLogList = logService.searchChargingLog(mongoLog);
					mongoLog = null;
					int logLength = mongoLogList.size();
					if (logLength > 0) {
						outputPower = new ArrayList<Object>();
						powerRate = new ArrayList<Object>();
						List<String> tempOutputPower = null;
						List<String> tempPowerRate = null;
						for (Log log : mongoLogList) {
							JSONObject messageJson = null;
							if (log.getMessage().indexOf("realTimeData") > 0) {
								messageJson = JSONObject.fromObject(log.getMessage().substring(log.getMessage().indexOf(":") + 1));
							} else {
								messageJson = JSONObject.fromObject(log.getMessage());
							}
							if (messageJson.has("equipmentData")) {
								JSONArray jsonArray = messageJson.getJSONArray("equipmentData");
								int jsonLength = jsonArray.size();
								JSONObject json = null;
								double outputPowerTotal = 0;
								map.put("outputMaxPower", jsonLength * 450);
								for (int j = 0; j < jsonLength; j++) {
									json = jsonArray.getJSONObject(j);
									if (json.has("outputVoltage") && !json.getString("outputVoltage").isEmpty() && json.has("outputElectric") && !json.getString("outputElectric").isEmpty()) {
										outputPowerTotal = outputPowerTotal + (Double.valueOf(json.getString("outputVoltage")) * Double.valueOf(json.getString("outputElectric")) / 1000);
									}
								}
								tempOutputPower = new ArrayList<String>();
								tempPowerRate = new ArrayList<String>();
								if (messageJson.has("requestDTTM")) {
									tempOutputPower.add(messageJson.getString("requestDTTM"));
									tempPowerRate.add(messageJson.getString("requestDTTM"));
								} else {
									tempOutputPower.add(Tools.date2Str(log.getTimestamp()));
									tempPowerRate.add(Tools.date2Str(log.getTimestamp()));
								}
								BigDecimal powerTotal = new BigDecimal(outputPowerTotal);
								tempOutputPower.add(String.valueOf(powerTotal.setScale(2, BigDecimal.ROUND_HALF_UP)));
								outputPower.add(tempOutputPower);
								BigDecimal outputPowerRate = new BigDecimal(outputPowerTotal / (jsonLength * 450) * 100);
								tempPowerRate.add(String.valueOf(outputPowerRate.setScale(2, BigDecimal.ROUND_HALF_UP)));
								powerRate.add(tempPowerRate);
								tempOutputPower = null;
								tempPowerRate = null;
							}
						}
					}
				}
				map.put("outputPower", outputPower);
				map.put("powerRate", powerRate);
				map.put("result", "Sucess");
				outputPower = null;
				powerRate = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩实时充电曲线信息获取异常:" + e.getMessage());
		}
		chargingStationStatus = null;
		mongoLog = null;
		mongoLogList = null;

		return map;
    }

	@RequestMapping(value = "realTimeChargingStation")
	@ResponseBody
    public Object getChargingStationStatus(ChargingStation chargingStation, String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		if (page == null || page.isEmpty()) {
			page = "1";
		}
		if (rows == null || rows.isEmpty()) {
			rows = "1";
		}
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		pageQuery.setTotalPage(0);
		pageQuery.setTotalResult(0);

		List<ChargingStation> chargingStationList = null;
		List<ChargingStationStatus> chargingStationStatusList = new ArrayList<ChargingStationStatus>();
		ChargingStationStatus chargingStationStatus = null;
		Map<String, Object> subStationMap = deviceObject.getSubStationMap();
		String chargingStationIDList = deviceObject.getChargingStationIDList();
		String chargingStationID = null;

		try {
			// 只监控已投运的站
//			chargingStation.setBulidStatus("3");
			if (chargingStationIDList != null) {
				chargingStation.setChargingStationIDList(chargingStationIDList.split(","));
			}
			chargingStation.setBranchCompanyID(this.getBranchCompanyID());
			chargingStationList = chargingStationService.getChargingStationStatusPage(chargingStation, pageQuery);
			for (ChargingStation chargingStationPage : chargingStationList) {
				chargingStationID = chargingStationPage.getChargingStationID();
				if (subStationMap != null && subStationMap.containsKey(chargingStationID)){
					chargingStationStatus = (ChargingStationStatus)subStationMap.get(chargingStationID);
					JSONArray equipmentData = chargingStationStatus.getEquipmentData();
					if (equipmentData != null) {
						int length = equipmentData.size();
						JSONObject equipmentJson = null;
						Map<String, Object> equipmentMap = null;
						String equipmentID = null;
						List<Object> equipmentStatusList = new ArrayList<Object>();
						double currentPowerTotal = 0;
						for (int i = 0; i < length; i++) {
							equipmentJson = equipmentData.getJSONObject(i);
							if (equipmentJson != null) {
								equipmentMap = new HashMap<String, Object>();
								if (equipmentJson.has("equipmentID")) {
									equipmentID = equipmentJson.getString("equipmentID");
									if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals("断开")) {
										equipmentMap.put("equipmentID", equipmentID);
										equipmentMap.put("equipmentStatus", "断开");
									} else {
										if (equipmentJson.has("deviceState")) {
											equipmentMap.put("equipmentID", equipmentID);
											equipmentMap.put("equipmentStatus", equipmentJson.getString("deviceState"));
											if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING) || equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGED)) {
												if (equipmentJson.has("outputVoltage") && !equipmentJson.getString("outputVoltage").isEmpty() && equipmentJson.has("outputElectric") && !equipmentJson.getString("outputElectric").isEmpty()) {
													currentPowerTotal = currentPowerTotal + (Double.valueOf(equipmentJson.getString("outputVoltage")) * Double.valueOf(equipmentJson.getString("outputElectric")) / 1000);
												}
											}
										} else {
											equipmentMap.put("equipmentID", equipmentID);
											equipmentMap.put("equipmentStatus", "断开");
										}
									}
									equipmentStatusList.add(equipmentMap);
								}
							}
						}
						BigDecimal outputPower = new BigDecimal(currentPowerTotal);
						chargingStationStatus.setCurrentPowerTotal(String.valueOf(outputPower.setScale(2, BigDecimal.ROUND_HALF_UP)));
						chargingStationStatus.setEquipmentStatusList(equipmentStatusList);
					}
					if ((chargingStationStatus.getOnLineCount() + chargingStationStatus.getOffLineCount()) == 0) {
						chargingStationStatus.setOffLineCount(chargingStationPage.getBgEquipmentCount() + chargingStationPage.getSmEquipmentCount());
					}
				} else {
					chargingStationStatus = new ChargingStationStatus();
					chargingStationStatus.setChargingStationID(chargingStationID);
					chargingStationStatus.setRequestDTTM(chargingStationPage.getRequestDTTM());
					chargingStationStatus.setOnlineStatus(false);
					chargingStationStatus.setOnLineCount(0);
					chargingStationStatus.setOffLineCount(chargingStationPage.getBgEquipmentCount() + chargingStationPage.getSmEquipmentCount());
					chargingStationStatus.setEquipmentStatusList(null);
					chargingStationStatus.setCurrentPowerTotal("0");
				}
				chargingStationStatus.setChargingStationNO(chargingStationPage.getChargingStationNO());
				chargingStationStatus.setChargingStationName(chargingStationPage.getChargingStationName());
				chargingStationStatus.setEquipmentCount(chargingStationPage.getBgEquipmentCount() + chargingStationPage.getSmEquipmentCount());
//				chargingStationStatus.setChargingDegreeDay(chargingStationPage.getChargingDegreeDay());
//				chargingStationStatus.setChargingCountDay(chargingStationPage.getChargingCountDay());
				chargingStationStatusList.add(chargingStationStatus);
				chargingStationStatus = null;
			}
			map.put("result", "Sucess");
			map.put("totalCnt", pageQuery.getTotalResult());
			map.put("totalPage", pageQuery.getTotalPage());
			map.put("chargingStationStatusList", chargingStationStatusList);

			if (subStationMap != null){
				chargingStationID = chargingStation.getChargingStationID();
				if (chargingStationID != null && !chargingStationID.isEmpty()) {
					map.put("totalOnlineCount", 0);
					map.put("totalOfflineCount", 0);
					if (subStationMap.containsKey(chargingStationID) && !"0".equals(chargingStation.getCommunicationState())) {
						map.put("totalOnlineCount", 1);
					} else if (!subStationMap.containsKey(chargingStationID) && !"1".equals(chargingStation.getCommunicationState())) {
						map.put("totalOfflineCount", 1);
					}
				} else {
					map.put("totalOnlineCount", subStationMap.size());
					map.put("totalOfflineCount", pageQuery.getTotalResult() - subStationMap.size());
					if ("0".equals(chargingStation.getCommunicationState())) {
						map.put("totalOnlineCount", 0);
						map.put("totalOfflineCount", pageQuery.getTotalResult());
					} else if ("1".equals(chargingStation.getCommunicationState())) {
						map.put("totalOfflineCount", 0);
					}

				}
				chargingStationID = null;
			} else {
				map.put("totalOnlineCount", 0);
				map.put("totalOfflineCount", pageQuery.getTotalResult());
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电站实时状态信息获取异常:" + e.getClass() + "," + e.getMessage());
		}
		chargingStationList = null;
		chargingStationStatusList = null;
		chargingStationStatus = null;
		chargingStationID = null;
		pageQuery = null;
		return map;
    }

	@RequestMapping(value = "realTimeChargingStationDetail")
	@ResponseBody
    public Object getChargingStationDetailStatus(ChargingStation chargingStation) {

		Map<String, Object> map = new HashMap<String, Object>();
		ChargingStationStatus chargingStationStatus = null;
		Map<String, Object> subStationMap = deviceObject.getSubStationMap();
		String chargingStationID = null;

		try {
			chargingStation.setBranchCompanyID(this.getBranchCompanyID());
			chargingStation = chargingStationService.getChargingStationDetailStatus(chargingStation);
			chargingStationID = chargingStation.getChargingStationID();
			if (subStationMap != null && subStationMap.containsKey(chargingStationID)){
				chargingStationStatus = (ChargingStationStatus)subStationMap.get(chargingStationID);
				JSONArray equipmentData = chargingStationStatus.getEquipmentData();
				if (equipmentData != null) {
					int length = equipmentData.size();
					JSONObject equipmentJson = null;
					Map<String, Object> equipmentMap = null;
					String equipmentID = null;
					List<Object> equipmentStatusList = new ArrayList<Object>();
					double currentPowerTotal = 0;
					for (int i = 0; i < length; i++) {
						equipmentJson = equipmentData.getJSONObject(i);
						if (equipmentJson != null) {
							equipmentMap = new HashMap<String, Object>();
							if (equipmentJson.has("equipmentID")) {
								equipmentID = equipmentJson.getString("equipmentID");
								if (equipmentJson.has("communicationState") && equipmentJson.getString("communicationState").equals("断开")) {
									equipmentMap.put("equipmentID", equipmentID);
									equipmentMap.put("equipmentStatus", "断开");
								} else {
									if (equipmentJson.has("deviceState")) {
										equipmentMap.put("equipmentID", equipmentID);
										equipmentMap.put("equipmentStatus", equipmentJson.getString("deviceState"));
										if (equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGING) || equipmentJson.getString("deviceState").equals(Const.EQUIPMENT_CHARGED)) {
											if (equipmentJson.has("outputVoltage") && !equipmentJson.getString("outputVoltage").isEmpty() && equipmentJson.has("outputElectric") && !equipmentJson.getString("outputElectric").isEmpty()) {
												currentPowerTotal = currentPowerTotal + (Double.valueOf(equipmentJson.getString("outputVoltage")) * Double.valueOf(equipmentJson.getString("outputElectric")) / 1000);
											}
										}
									} else {
										equipmentMap.put("equipmentID", equipmentID);
										equipmentMap.put("equipmentStatus", "断开");
									}
								}
								equipmentStatusList.add(equipmentMap);
							}
						}
					}
					BigDecimal outputPower = new BigDecimal(currentPowerTotal);
					chargingStationStatus.setCurrentPowerTotal(String.valueOf(outputPower.setScale(2, BigDecimal.ROUND_HALF_UP)));
					chargingStationStatus.setEquipmentStatusList(equipmentStatusList);
				}
				if ((chargingStationStatus.getOnLineCount() + chargingStationStatus.getOffLineCount()) == 0) {
					chargingStationStatus.setOffLineCount(chargingStation.getBgEquipmentCount() + chargingStation.getSmEquipmentCount());
				}
			} else {
				chargingStationStatus = new ChargingStationStatus();
				chargingStationStatus.setChargingStationID(chargingStationID);
				chargingStationStatus.setRequestDTTM(chargingStation.getRequestDTTM());
				chargingStationStatus.setOnlineStatus(false);
				chargingStationStatus.setOnLineCount(0);
				chargingStationStatus.setOffLineCount(chargingStation.getBgEquipmentCount() + chargingStation.getSmEquipmentCount());
				chargingStationStatus.setEquipmentStatusList(null);
				chargingStationStatus.setCurrentPowerTotal("0");
			}
			chargingStationStatus.setChargingStationNO(chargingStation.getChargingStationNO());
			chargingStationStatus.setChargingStationName(chargingStation.getChargingStationName());
			chargingStationStatus.setEquipmentCount(chargingStation.getBgEquipmentCount() + chargingStation.getSmEquipmentCount());
			chargingStationStatus.setChargingDegreeDay(chargingStation.getChargingDegreeDay());
			chargingStationStatus.setChargingCountDay(chargingStation.getChargingCountDay());
			map.put("result", "Sucess");
			map.put("chargingStationStatus", chargingStationStatus);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电站实时状态信息获取异常:" + e.getClass() + "," + e.getMessage());
		}
		chargingStationStatus = null;
		chargingStationID = null;
		return map;
    }

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/realTimeChargingStationStatistics")
	@ResponseBody
	public Object getChargingStationStatistics(String type, String chargingStationID, HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
//		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取充电站统计信息开始");

		try {
			if (type == null) {
				ChargingStation chargingStation = new ChargingStation();
				chargingStation.setBranchCompanyID(this.getBranchCompanyID());
				List<ChargingStation> chargingStationList = chargingStationService.getChargingStationMapInfo(chargingStation);
				File folder = null;
				List<String> fileNameList = null;
				for (ChargingStation tempChargingStation : chargingStationList) {
					folder = new File(Const.SYSTEMCONFIG_PATH_BUS + tempChargingStation.getChargingStationID() + File.separator);
					fileNameList = new ArrayList<String>();
					if (folder.exists()) {
						File[] files = folder.listFiles();
						for (File imageFile : files) {
							fileNameList.add(imageFile.getName());
						}
					}
					tempChargingStation.setChargingStationImageName(fileNameList);
					folder = null;
					fileNameList = null;
				}
				map.put("chargingStationList", chargingStationList);
				chargingStationList = null;
				chargingStation = null;
			}

			List<ChargingStationStatistics> chargingStationStatisticsList = null;
			if (deviceObject.getSessionMap() != null && deviceObject.getSessionMap().get(Const.SESSION_CHARGINGSTATIONSTATISTICS) != null) {
				chargingStationStatisticsList = (List<ChargingStationStatistics>)deviceObject.getSessionMap().get(Const.SESSION_CHARGINGSTATIONSTATISTICS);
			}
			List<ChargingStationStatistics> chargingStationStatisticsListThis = null;
			ChargingStationStatistics chargingStationStatistics = new ChargingStationStatistics();
			if (chargingStationStatisticsList == null || chargingStationStatisticsList.size() == 0) {
				chargingStationStatisticsList=  chargingStationStatisticsService.getChargingStationStatisticsList(chargingStationStatistics);
			} else {
				chargingStationStatistics.setLastSearchID(chargingStationStatisticsList.get(0).getLastSearchID());
				chargingStationStatisticsListThis =  chargingStationStatisticsService.getChargingStationStatisticsList(chargingStationStatistics);
			}
			if (chargingStationStatisticsList.size() > 0) {
				if (chargingStationStatisticsListThis != null && chargingStationStatisticsListThis.size() > 0) {
					String tempChargingStationID = null;
					int k = 0;
					for (ChargingStationStatistics chargingStationStatisticsThis : chargingStationStatisticsListThis) {
						tempChargingStationID = "";
						for (ChargingStationStatistics tempChargingStationStatistics : chargingStationStatisticsList) {
							if (chargingStationStatisticsThis.getChargingStationID().equals(tempChargingStationStatistics.getChargingStationID())) {
								tempChargingStationID = chargingStationStatisticsThis.getChargingStationID();
								if (Tools.getDateDiff(new Date(), tempChargingStationStatistics.getLastSearchTime(), "DAY") > 0) {
									tempChargingStationStatistics.setChargingCountDay(chargingStationStatisticsThis.getChargingCountDay());
									tempChargingStationStatistics.setChargingDegreeDay(chargingStationStatisticsThis.getChargingDegreeDay());
								} else {
									tempChargingStationStatistics.setChargingCountDay(tempChargingStationStatistics.getChargingCountDay() + chargingStationStatisticsThis.getChargingCountDay());
									tempChargingStationStatistics.setChargingDegreeDay(tempChargingStationStatistics.getChargingDegreeDay() + chargingStationStatisticsThis.getChargingDegreeDay());
								}
								if (Tools.getDateDiff(new Date(), tempChargingStationStatistics.getLastSearchTime(), "MONTH") > 0) {
									tempChargingStationStatistics.setChargingCountMonth(chargingStationStatisticsThis.getChargingCountMonth());
									tempChargingStationStatistics.setChargingDegreeMonth(chargingStationStatisticsThis.getChargingDegreeMonth());
								} else {
									tempChargingStationStatistics.setChargingCountMonth(tempChargingStationStatistics.getChargingCountMonth() + chargingStationStatisticsThis.getChargingCountMonth());
									tempChargingStationStatistics.setChargingDegreeMonth(tempChargingStationStatistics.getChargingDegreeMonth() + chargingStationStatisticsThis.getChargingDegreeMonth());
								}
								tempChargingStationStatistics.setChargingCountTotal(tempChargingStationStatistics.getChargingCountTotal() + chargingStationStatisticsThis.getChargingCountTotal());
								tempChargingStationStatistics.setChargingDegreeTotal(tempChargingStationStatistics.getChargingDegreeTotal() + chargingStationStatisticsThis.getChargingDegreeTotal());
								tempChargingStationStatistics.setEquipmentDCCount(chargingStationStatisticsThis.getEquipmentDCCount());
								tempChargingStationStatistics.setLastSearchTime(chargingStationStatisticsThis.getLastSearchTime());
								tempChargingStationStatistics.setLastSearchID(chargingStationStatisticsThis.getLastSearchID());
								break;
							}
						}
						if (tempChargingStationID.isEmpty()) {
							chargingStationStatisticsList.add(chargingStationStatisticsListThis.get(k));
						}
						k++;
					}
					Collections.sort(chargingStationStatisticsList);
				}
				chargingStationStatisticsListThis = null;

				String [] chargingStationIDList = null;
				if(chargingStationID != null && !chargingStationID.isEmpty()){
					chargingStationIDList = chargingStationID.split(",");
				}
				if (chargingStationIDList != null) {
					int length = chargingStationIDList.length;
					for (int i = 0; i < length; i++) {
						for (ChargingStationStatistics tempChargingStationStatistics : chargingStationStatisticsList) {
							if (chargingStationIDList[i].equals(tempChargingStationStatistics.getChargingStationID()) && ((this.getBranchCompanyID() == null || this.getBranchCompanyID().isEmpty()) ||  (tempChargingStationStatistics.getBranchCompanyID() != null && tempChargingStationStatistics.getBranchCompanyID().equals(this.getBranchCompanyID())))) {
								chargingStationStatistics.setChargingStationCount(chargingStationStatistics.getChargingStationCount() + 1);
								chargingStationStatistics.setEquipmentDCCount(chargingStationStatistics.getEquipmentDCCount() + tempChargingStationStatistics.getEquipmentDCCount());
								chargingStationStatistics.setChargingCountDay(chargingStationStatistics.getChargingCountDay() + tempChargingStationStatistics.getChargingCountDay());
								chargingStationStatistics.setChargingDegreeDay(chargingStationStatistics.getChargingDegreeDay() + tempChargingStationStatistics.getChargingDegreeDay());
								chargingStationStatistics.setChargingCountMonth(chargingStationStatistics.getChargingCountMonth() + tempChargingStationStatistics.getChargingCountMonth());
								chargingStationStatistics.setChargingDegreeMonth(chargingStationStatistics.getChargingDegreeMonth() + tempChargingStationStatistics.getChargingDegreeMonth());
								chargingStationStatistics.setChargingCountTotal(chargingStationStatistics.getChargingCountTotal() + tempChargingStationStatistics.getChargingCountTotal());
								chargingStationStatistics.setChargingDegreeTotal(chargingStationStatistics.getChargingDegreeTotal() + tempChargingStationStatistics.getChargingDegreeTotal());
								break;
							}
						}
					}
				} else {
					for (ChargingStationStatistics tempChargingStationStatistics : chargingStationStatisticsList) {
						if ((this.getBranchCompanyID() == null || this.getBranchCompanyID().isEmpty()) ||  (tempChargingStationStatistics.getBranchCompanyID() != null && tempChargingStationStatistics.getBranchCompanyID().equals(this.getBranchCompanyID()))) {
							chargingStationStatistics.setChargingStationCount(chargingStationStatistics.getChargingStationCount() + 1);
							chargingStationStatistics.setEquipmentDCCount(chargingStationStatistics.getEquipmentDCCount() + tempChargingStationStatistics.getEquipmentDCCount());
							chargingStationStatistics.setChargingCountDay(chargingStationStatistics.getChargingCountDay() + tempChargingStationStatistics.getChargingCountDay());
							chargingStationStatistics.setChargingDegreeDay(chargingStationStatistics.getChargingDegreeDay() + tempChargingStationStatistics.getChargingDegreeDay());
							chargingStationStatistics.setChargingCountMonth(chargingStationStatistics.getChargingCountMonth() + tempChargingStationStatistics.getChargingCountMonth());
							chargingStationStatistics.setChargingDegreeMonth(chargingStationStatistics.getChargingDegreeMonth() + tempChargingStationStatistics.getChargingDegreeMonth());
							chargingStationStatistics.setChargingCountTotal(chargingStationStatistics.getChargingCountTotal() + tempChargingStationStatistics.getChargingCountTotal());
							chargingStationStatistics.setChargingDegreeTotal(chargingStationStatistics.getChargingDegreeTotal() + tempChargingStationStatistics.getChargingDegreeTotal());
						}
					}
				}
			}
			if (deviceObject.getSessionMap() != null) {
				deviceObject.getSessionMap().put(Const.SESSION_CHARGINGSTATIONSTATISTICS, chargingStationStatisticsList);
			} else {
				Map<String, Object> sessionMap = new HashMap<String, Object>();
				sessionMap.put(Const.SESSION_CHARGINGSTATIONSTATISTICS, chargingStationStatisticsList);
				deviceObject.setSessionMap(sessionMap);
				sessionMap = null;
			}
			map.put("chargingStationStatistics", chargingStationStatistics);
			chargingStationStatistics = null;
			chargingStationStatisticsList = null;
//			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取充电站统计信息结束");
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取充电站统计信息异常：" + e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/operateEquipment")
	@ResponseBody
	public Object sendOrder(String equipmentID, String flag) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备操作" + flag + "开始");
		try{
			Socket client = MessageRegistration.find(equipmentID);
			Class<?> val= StatuteManagement.findStatute(equipmentService.getEquipmentTypeByEquipmentCode(equipmentID).getAgreementID()).getStatuteCls();
	        Method method = null;
	        if("start".equals(flag)) {
	            method = val.getMethod("start");
	        } else {
	            method = val.getMethod("stop");
	        }
	        byte[] sendBuffer = (byte[])method.invoke(val.newInstance());
	        if (client != null) {
	            OutputStream out = client.getOutputStream();
	            out = client.getOutputStream();
	            out.write(sendBuffer);
	            map.put("result", "Sucess");
	        } else {
				map.put("result", "NotFound");
	        }
		} catch (Exception e){
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备操作" + flag + "异常:" + e.getMessage());
			map.put("result", "Error");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备操作" + flag + "结束");
		return map;
	}
}
